package com.jy.api.controller.admin;

import com.jy.api.service.KitchenPrintService;
import com.jy.api.signUpReq.admin.KitchenPrintSignUpReq;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * Created by lihu on 2017/11/14.
 */
@Slf4j
@RestController
@RequestMapping(value = "v1/admin", produces = "application/json;charset=utf-8")
public class KitchenPrintConfigController {

    @Autowired
    KitchenPrintService kitchenPrintService;

    /**
     * 打印机设置
     *
     * @param kitchenPrintSignUpReq
     * @param bindingResult
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, value = "/kitchen-print")
    public ResponseEntity saveOrUpdate(@Valid @RequestBody KitchenPrintSignUpReq kitchenPrintSignUpReq,
                                       BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            log.error("【厨房设置】参数不正确, materialSignUpReq={}", kitchenPrintSignUpReq);
            return new ResponseEntity(bindingResult.getFieldError().getDefaultMessage(), HttpStatus.BAD_REQUEST);
        }
        kitchenPrintService.saveOrUpdate(kitchenPrintSignUpReq);
        return new ResponseEntity(HttpStatus.OK);
    }


    @PreAuthorize("hasRole('ADMIN')")
    @RequestMapping(method = RequestMethod.GET, value = "/{storeId}/kitchen-print")
    public ResponseEntity getKitchenPrint(@PathVariable String storeId) {
        return new ResponseEntity(kitchenPrintService.getKitchenPrintConfig(storeId), HttpStatus.OK);
    }
}
